package com.jqing.lib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.jqing.lib.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chshqiang on 2014/5/5.
 * 邮箱自动补全控件
 */
public class AutoCompleteEmailText extends AutoCompleteTextView {

    private AutoCompleteEmailAdapter adapter;
    private String[] emailSuffix;

    public AutoCompleteEmailText(Context context) {
        super(context);
        init(context);
    }

    public AutoCompleteEmailText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoCompleteEmailText);
        int suffixId = a.getResourceId(R.styleable.AutoCompleteEmailText_suffix, R.array.default_mail_suffix);
        emailSuffix = context.getResources().getStringArray(suffixId);
        a.recycle();
        init(context);
    }

    private void init(Context context) {
        adapter = new AutoCompleteEmailAdapter(context);
        setAdapter(adapter);

        addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                textChanged(s);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }
        });
        // default=2 当输入一个字符的时候就开始检测
        setThreshold(3);
    }

    public void textChanged(Editable s) {
        String input = s.toString();
        adapter.mList.clear();
        if (input.length() >= 3 && "@".equals(input.substring(input.length() - 1, input.length()))) {
            for (String suffix : emailSuffix) {
                adapter.mList.add(input + suffix);
            }
        }
        adapter.notifyDataSetChanged();
        showDropDown();
    }

    public void setEmailSuffix(String[] emailSuffix) {
        this.emailSuffix = emailSuffix;
    }

    class AutoCompleteEmailAdapter extends BaseAdapter implements Filterable {
        List<String> mList;
        private Context mContext;
        private MyFilter mFilter;

        public AutoCompleteEmailAdapter(Context context) {
            mContext = context;
            mList = new ArrayList<String>();
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList == null ? null : mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                TextView tv = new TextView(mContext);
                tv.setBackgroundColor(0xffe8e6e6);
                tv.setTextColor(0xffaaaaaa);
                tv.setTextSize(20);
                tv.setPadding(15, 0, 0, 8);
                convertView = tv;
            }
            TextView txt = (TextView) convertView;
            txt.setText(mList.get(position));
            return txt;
        }

        @Override
        public Filter getFilter() {
            if (mFilter == null) {
                mFilter = new MyFilter();
            }
            return mFilter;
        }

        private class MyFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                if (mList == null) {
                    mList = new ArrayList<String>();
                }
                results.values = mList;
                results.count = mList.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        }
    }
}
